/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * EditorInterfaceChooser.java
 *
 * Created on 05/10/2010, 09:46:21
 */

package vnetlab.ui.editor;

import java.awt.Component;
import java.awt.Dimension;
import java.awt.GridLayout;
import javax.swing.JRadioButton;
import javax.swing.border.TitledBorder;
import vnetlab.elements.Computer;
import vnetlab.elements.Element;
import vnetlab.elements.Router;


/**
 *
 * @author rogeriosv
 */
public class EditorInterfaceChooser extends javax.swing.JDialog {
    /** A return status code - returned if Cancel button has been pressed */
    public static final int RET_CANCEL = 0;
    /** A return status code - returned if OK button has been pressed */
    public static final int RET_OK = 1;

    private Element sourceElement;
    private Element targetElement;

    /** Creates new form EditorInterfaceChooser */
    public EditorInterfaceChooser(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        setLocationRelativeTo(null);
    }

    public EditorInterfaceChooser(Element source, Element target, java.awt.Frame parent, boolean modal){
        this(parent, modal);
        jPanelSource.setLayout( new GridLayout(source.numberOfNIC(), 1));
        jPanelTarget.setLayout( new GridLayout(target.numberOfNIC(),1));
        setElements(source, target);
    }
    
    public EditorInterfaceChooser(Element source, Element target) {
        this(null, true);
        jPanelSource.setLayout( new GridLayout(source.numberOfNIC(), 1));
        jPanelTarget.setLayout( new GridLayout(target.numberOfNIC(),1));
        setElements(source, target);
    }

    private void setElements(Element source, Element target){
        sourceElement = source;
        targetElement = target;
        insertRadios();
    }

    private void insertRadios() {

        //Cria os radios do source
        ((TitledBorder)jPanelSource.getBorder()).setTitle(sourceElement.getName());
        for(int i=0; i < sourceElement.numberOfNIC(); i++){
            JRadioButton radioButton = new JRadioButton("NIC "+i);

            if(!sourceElement.isConected(i))
                radioButton.setSelected(true);
            else
                radioButton.setEnabled(false);
            
            buttonGroupSource.add(radioButton);
            jPanelSource.add(radioButton);
        }

        //Cria os radios do target
        ((TitledBorder)jPanelTarget.getBorder()).setTitle(targetElement.getName());
        for(int i=0; i < targetElement.numberOfNIC(); i++){
            JRadioButton radioButton = new JRadioButton("NIC "+i);
            
            if(!targetElement.isConected(i))
                radioButton.setSelected(true);
            else
                radioButton.setEnabled(false);

            buttonGroupTarget.add(radioButton);
            jPanelTarget.add(radioButton);
        }

        //Atualiza o tamanho da janela
        int height = jPanelSource.getWidth() > jPanelTarget.getWidth() ? jPanelSource.getWidth()  : jPanelTarget.getWidth();
        setSize((int)getSize().getWidth(), height + 250);

        setResizable(false);
    }

    public int getTargetInterface(){

        for(Component c :jPanelTarget.getComponents())
            if(c instanceof JRadioButton)
                if (((JRadioButton)c).isSelected())
                    return (Integer.parseInt(((JRadioButton)c).getText().replace("NIC ", "")));

        return -1;
    }
    
    public int getSourceInterface(){

        for(Component c :jPanelSource.getComponents())
            if(c instanceof JRadioButton)
                if (((JRadioButton)c).isSelected())
                    return (Integer.parseInt(((JRadioButton)c).getText().replace("NIC ", "")));
        
        return -1;
    }


    /** @return the return status of this dialog - one of RET_OK or RET_CANCEL */
    public int getReturnStatus() {
        return returnStatus;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroupSource = new javax.swing.ButtonGroup();
        buttonGroupTarget = new javax.swing.ButtonGroup();
        okButton = new javax.swing.JButton();
        cancelButton = new javax.swing.JButton();
        jPanelSource = new javax.swing.JPanel();
        jPanelTarget = new javax.swing.JPanel();

        setTitle("Interfaces Chooser");
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                closeDialog(evt);
            }
        });

        okButton.setText("OK");
        okButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                okButtonActionPerformed(evt);
            }
        });

        cancelButton.setText("Cancel");
        cancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelButtonActionPerformed(evt);
            }
        });

        jPanelSource.setBorder(javax.swing.BorderFactory.createTitledBorder("Source"));
        jPanelSource.setMinimumSize(new java.awt.Dimension(100, 130));

        javax.swing.GroupLayout jPanelSourceLayout = new javax.swing.GroupLayout(jPanelSource);
        jPanelSource.setLayout(jPanelSourceLayout);
        jPanelSourceLayout.setHorizontalGroup(
            jPanelSourceLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 78, Short.MAX_VALUE)
        );
        jPanelSourceLayout.setVerticalGroup(
            jPanelSourceLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 107, Short.MAX_VALUE)
        );

        jPanelTarget.setBorder(javax.swing.BorderFactory.createTitledBorder("Target"));

        javax.swing.GroupLayout jPanelTargetLayout = new javax.swing.GroupLayout(jPanelTarget);
        jPanelTarget.setLayout(jPanelTargetLayout);
        jPanelTargetLayout.setHorizontalGroup(
            jPanelTargetLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 81, Short.MAX_VALUE)
        );
        jPanelTargetLayout.setVerticalGroup(
            jPanelTargetLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 107, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jPanelSource, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(okButton, javax.swing.GroupLayout.PREFERRED_SIZE, 67, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(cancelButton)
                    .addComponent(jPanelTarget, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {cancelButton, okButton});

        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jPanelTarget, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanelSource, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cancelButton)
                    .addComponent(okButton))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void okButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_okButtonActionPerformed
        doClose(RET_OK);
    }//GEN-LAST:event_okButtonActionPerformed

    /** Closes the dialog */
    private void closeDialog(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_closeDialog
        doClose(RET_CANCEL);
    }//GEN-LAST:event_closeDialog

    private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButtonActionPerformed
        doClose(RET_CANCEL);
}//GEN-LAST:event_cancelButtonActionPerformed

    private void doClose(int retStatus) {
        returnStatus = retStatus;
        setVisible(false);
        dispose();
    }

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {

                Element e1 = new Computer(null, null, null, null);
                Element e2 = new Router(null, null, null, null);
                EditorInterfaceChooser dialog = new EditorInterfaceChooser(e1, e2);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.ButtonGroup buttonGroupSource;
    private javax.swing.ButtonGroup buttonGroupTarget;
    private javax.swing.JButton cancelButton;
    private javax.swing.JPanel jPanelSource;
    private javax.swing.JPanel jPanelTarget;
    private javax.swing.JButton okButton;
    // End of variables declaration//GEN-END:variables

    private int returnStatus = RET_CANCEL;
}
